//
//  WLKPageCotrol.m
//  我DE轮播图
//
//  Created by mac on 2017/7/29.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "WLKPageCotrol.h"

#define KWidth_page [[UIScreen mainScreen]bounds].size.width
@implementation WLKPageCotrol

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//- (instancetype)initWithFrame:(CGRect)frame withpageImageArr:(NSArray *)arr withTotalCount:(NSInteger)toatlCount{
//    self = [super initWithFrame:frame];
//    if (self) {
//        self.imeArr = arr;
//        self.totalCount = toatlCount;
//         [self setupUI];//
//    }
//    return self;
//}
//- (void)layoutSubviews{
//
////    self.imeArr = arr;
////    self.totalCount = toatlCount;
//    [self setupUI];//
//}
- (void)setupUI{

//    if (!self.pageView) {
//        self.pageView = [[UIView alloc] init];
//        self.pageView.frame = CGRectMake((KWidth - (8+2)*(self.totalCount - 1) + 45) / 2, 0, (8+2)*(self.totalCount - 1) + 45, 10);
//        self.pageView.backgroundColor = [UIColor redColor];
        [self addSubview:self.pageView];
        self.pageView.sd_layout.topSpaceToView(self, 0).widthIs((8+2)*(self.totalCount - 1) + 45).heightIs(10).centerXIs(KWidth / 2).centerYIs(self.height / 2) ;
        [self ceratPageView:0]; 
//    }
}
- (UIView *)pageView{

    if (!_pageView) {
        _pageView = [[UIView alloc] init];
//        self.pageView.backgroundColor = [UIColor redColor];
    }
    return _pageView;
}
- (void)ceratPageView:(NSInteger)integerpage{
    NSInteger wide = 0;
//    NSLog(@"integerpage = %ld", (long)integerpage);
    for (int i = 0; i < self.totalCount; i++) {
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 1, 0, 0)];
        //        self.imageView.backgroundColor = [UIColor redColor];
        self.imageView.tag = i;
        
        [self.pageView addSubview:self.imageView];
        
        if (i == integerpage) {
            //            wide = wide - (8+2) +20;
            if (self.imeArr) {
                self.imageView.image = [UIImage imageNamed:self.imeArr[0]];
            }else{
                self.imageView.backgroundColor = [UIColor whiteColor];
            }
            
            self.imageView.sd_layout.topSpaceToView(self.pageView, 1).leftSpaceToView(self.pageView, wide).bottomSpaceToView(self.pageView, 1).widthIs(45);
            wide += 47 ;
        }else{
            if (self.imeArr) {
                self.imageView.image = [UIImage imageNamed:self.imeArr[1]];
            }else{
                self.imageView.backgroundColor = [UIColor grayColor];
            }
            self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2;
            self.imageView.layer.masksToBounds = YES;
            self.imageView.sd_layout.topSpaceToView(self.pageView, 1).leftSpaceToView(self.pageView, wide).bottomSpaceToView(self.pageView, 1).widthIs(8);
            wide += 10 ;
        }
    }
}
- (void)setCurrentPage:(NSInteger)inter{
    for(UIImageView *view in [self.pageView subviews])
    {
        [view removeFromSuperview];
    }
//[self.pageView.subviews makeObjectsPerformSelector:@selector(removeAllObjects)];
    [self ceratPageView:inter];
}
@end
